//
//  XDStatusOriginalView.swift
//  weibo
//
//  Created by lucy on 2016/10/26.
//  Copyright © 2016年 com.huashengHuaSheng. All rights reserved.
//

import UIKit

class XDStatusOriginalView: UIView {
    //记录原厂微博的地步约束
    var originalViewBottomeCons: Constraint?

    var statusViewModel: XDStatusViewModel?{
        didSet{
            // 当前的控件赋值了
            // 头像
            headImageView.xd_setImage(imageWithURl: statusViewModel?.statusModel?.user?.profile_image_url, placeHolderImageName: "avatar_default_big")
            // 昵称
            nameLabel.text = statusViewModel?.statusModel?.user?.name
            // 等级
            membershipImageView.image = statusViewModel?.mbrankImage
            // 认证
            avatarImageView.image = statusViewModel?.verifiedImage
            // 微博正文
            contentLabel.text = statusViewModel?.statusModel?.text
            
            //下载约束
            originalViewBottomeCons?.uninstall()
            //有配图
            if (statusViewModel?.statusModel?.pic_urls?.count)!>0 {
                pictureView.picUrls = statusViewModel?.statusModel?.pic_urls
                pictureView.isHidden = false
                self.snp_makeConstraints{ (make) in
                    self.originalViewBottomeCons = make.bottom.equalTo(pictureView).offset(10).constraint
                }
            }else{
                //没有配图
                pictureView.isHidden = true
                self.snp_makeConstraints{ (make) in
                self.originalViewBottomeCons = make.bottom.equalTo(contentLabel).offset(10).constraint
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 设置视图
    private func setupUI(){
//        backgroundColor = RandomColor()
        backgroundColor = UIColor.white
        // 01 - 添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(membershipImageView)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        // 02 - 添加约束
        headImageView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 35, height: 35))
            make.top.left.equalTo(10)
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView)
            make.left.equalTo(headImageView.snp_right).offset(10)
        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp_right).offset(10)
        }
        
        membershipImageView.snp_makeConstraints { (make) in
            make.centerY.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(10)
        }
        
        avatarImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp_right)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.left.equalTo(headImageView)
            //            make.right.equalTo(self).offset(-10)
//            make.bottom.equalTo(self).offset(-10)
        }
        
        pictureView.snp_makeConstraints { (make) in
//            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.left.equalTo(contentLabel)
        }
        
        self.snp_makeConstraints { (make) in
            self.originalViewBottomeCons = make.bottom.equalTo(pictureView).offset(10).constraint
        }

    }
    
    // MARK: - 懒加载控件
    // 01 头像
    private lazy var headImageView: UIImageView = UIImageView(imageViewName: "avatar_default_big")
    // 02 昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.blue, textSize: XDNormalSizeFont, textContent: "老宋")
    // 03 时间
    private lazy var timeLabel: UILabel = UILabel(textColor: THEMECOLOR, textSize: XDNormalSizeFont, textContent: "时间")
    // 04 来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: THEMECOLOR, textSize: XDNormalSizeFont, textContent: "来自黑马21期大神来自黑马21期大神来自黑马21期大神")
    // 05 微博等级
    private lazy var membershipImageView: UIImageView = UIImageView(imageViewName: "common_icon_membership")
    // 06 认证
    private lazy var avatarImageView: UIImageView =  UIImageView(imageViewName: "avatar_vgirl")
    // 07 微博正文
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.black, textSize: XDNormalSizeFont, textContent: "", maxWidth: XDSCREENW-20)
    //08 配图
    private lazy var pictureView : XDStatusPictureView = {
        let view = XDStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view

    }()

}

























